package edu.upb.acs.jobsmaps.activity.details;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.upb.acs.jobsmaps.AddressLookup;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.database.task.GetCompanyAfterIdTask;
import edu.upb.acs.jobsmaps.database.task.GetJobAfterIdTask;
import edu.upb.acs.jobsmaps.entity.Company;
import edu.upb.acs.jobsmaps.entity.Job;

public class ViewJobDetailsActivity extends Activity {
	private static final String TAG = ViewJobDetailsActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.job_details);

		Log.d(TAG, "Initialize activity");
	}

	@Override
	protected void onStart(){
		super.onStart();
		Log.d(TAG, "Start activity");

		Bundle extras = getIntent().getExtras();
		String id= "";
		if (extras != null) {
			id = extras.getString("job_id");
			Log.d(TAG, "Show details for job with id = " + id);
		}else {
			Log.e(TAG, "No job id received");
			throw new RuntimeException("Called ViewJobDetailsActivity without job id");
		}

		Job job = null;
		GetJobAfterIdTask getJobAfterIdTask = new GetJobAfterIdTask();
		try {
			job = getJobAfterIdTask.execute(id).get();
			Log.d(TAG, "Got job = " + job);
		} catch (Exception e) {
			Log.e(TAG, "Can not get job", e);
		}

		if (job == null){
			Log.e(TAG, "No job found. Return....");
			//TODO display error message in the UI
			return;
		}

		Company company = null;
		GetCompanyAfterIdTask getCompanyAfterIdTask = new GetCompanyAfterIdTask();
		try {
			company = getCompanyAfterIdTask.execute(job.getCompanyId()).get();
			Log.d(TAG, "Got company = " + company);
		} catch (Exception e) {
			Log.e(TAG, "Can not get company", e);
		}

		if (company == null){
			Log.e(TAG, "No company found. Return....");
			//TODO display error message in the UI
			return;
		}

		TextView jobTitle = (TextView) findViewById(R.id.jobTitle);
		jobTitle.setText(job.getName());
		jobTitle.setPaintFlags(jobTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

		TextView companyName = (TextView) findViewById(R.id.jobCompanyName);
		companyName.setText(company.getName());

		TextView companyProfile = (TextView) findViewById(R.id.jobCompanyProfile);
		companyProfile.setText(company.getProfile());

		AddressLookup addressLookup = new AddressLookup(job.getLatitude(), job.getLongitude(), getApplicationContext() );
		TextView jobAddress = (TextView) findViewById(R.id.jobAddress);
		jobAddress.setText(addressLookup.getAddress());

		//crop if the text is longer than 30 characters
		if (null != job.getDescription()) {
			TextView jobDescription = (TextView) findViewById(R.id.jobDescription);
			StringBuilder description = new StringBuilder(job.getDescription());
			jobDescription.setText(Html.fromHtml(description.toString()));
		}

		TextView jobPostedDate = (TextView) findViewById(R.id.postedDate);
		jobPostedDate.setText(job.getPostedDate().toString());

		Button button = (Button) findViewById(R.id.viewJobDetails);

		final Job finalJob = job;
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent internetIntent = new Intent(Intent.ACTION_VIEW);
				internetIntent.setDataAndType(Uri.parse(finalJob.getJobUrl()), "text/html");
				internetIntent.addCategory(Intent.CATEGORY_BROWSABLE);
				startActivity(internetIntent);
			}
		});
	}

}
